/**
 * goio-util
 * 
 * This class do
 * 
 * @link http://www.marcosperon.com/
 * @copyright 2009 - Peron Co.
 * @author Marcos Peron
 * @package com.marcosperon.util.io
 * @version 2009
 */
package com.mperon.util.io;

import java.io.InputStream;
import java.net.URL;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * The Class ResourceLocator.
 */
class ResourceLocator {

    /** The Constant CLASS_EXTENSION. */
    public static final String CLASS_EXTENSION = ".class";

    /** The Constant FILE_SUFFIX. */
    public static final String FILE_SUFFIX = ".properties";

    /**
     * Gets the resource bundle.
     * 
     * @param name the name
     * 
     * @return the resource bundle
     */
    public static ResourceBundle getResourceBundle(final String name) {
        return ResourceLocator.getResourceBundle(name, Thread.currentThread()
                .getContextClassLoader(), Locale.getDefault());
    }

    /**
     * Gets the resource bundle.
     * 
     * @param name the name
     * @param loader the loader
     * 
     * @return the resource bundle
     */
    public static ResourceBundle getResourceBundle(final String name, final ClassLoader loader) {
        return ResourceLocator.getResourceBundle(name, loader, Locale.getDefault());
    }

    /**
     * Gets the resource bundle.
     * 
     * @param name the name
     * @param locale the locale
     * 
     * @return the resource bundle
     */
    public static ResourceBundle getResourceBundle(final String name, final Locale locale) {
        return ResourceLocator.getResourceBundle(name, Thread.currentThread()
                .getContextClassLoader(), locale);
    }

    /**
     * Gets the resource bundle.
     * 
     * @param name the name
     * @param loader the loader
     * @param locale the locale
     * 
     * @return the resource bundle
     */
    public static ResourceBundle getResourceBundle(String name, final ClassLoader loader,
            final Locale locale) {
        if (name == null) {
            throw new IllegalArgumentException("null input: name");
        }
        if (name.startsWith("/")) {
            name = name.substring(1);
        }
        if (name.endsWith(FILE_SUFFIX)) {
            name = name.substring(0, name.length() - FILE_SUFFIX.length());
        }
        name = name.replace('/', '.');
        // Throws MissingResourceException on lookup failures:
        ResourceBundle rb;
        try {
            rb = ResourceBundle.getBundle(name, locale, loader);
        } catch (final MissingResourceException e) {
            rb = null;
            throw new ResourceLocatorException("The Resource Bundle with name " + name
                    + " was not found!", e);
        }
        return rb;
    }

    /**
     * Gets the resource stream.
     * 
     * @param name the name
     * 
     * @return the resource stream
     */
    public static InputStream getResourceStream(final String name) {
        return ResourceLocator.getResourceStream(name, Thread.currentThread()
                .getContextClassLoader());
    }

    /**
     * Gets the resource stream.
     * 
     * @param name the name
     * @param loader the loader
     * 
     * @return the resource stream
     */
    public static InputStream getResourceStream(String name, ClassLoader loader) {
        if (name == null) {
            throw new IllegalArgumentException("null input: name");
        }
        InputStream result = null;
        if (loader == null) {
            loader = ClassLoader.getSystemClassLoader();
        }
        name = name.replace('.', '/');
        if (!name.endsWith(FILE_SUFFIX)) {
            name = name.concat(FILE_SUFFIX);
        }
        // Returns null on lookup failures:
        result = loader.getResourceAsStream(name);
        if (result == null) {
            throw new ResourceLocatorException("The Resource file with name " + name
                    + " was not found!");
        }
        return result;
    }

    /**
     * Gets the resource stream.
     * 
     * @param clazz the clazz
     * @param file the file
     * 
     * @return the resource stream
     */
    public static InputStream getResourceStream(final Class<?> clazz, final String file) {
        return clazz.getResourceAsStream(file);
    }

    /**
     * Gets the resource path.
     * 
     * @param classe the classe
     * @param file the file
     * 
     * @return the resource path
     */
    public static String getResourcePath(final Class<?> classe, final String file) {
        return ResourceLocator.getClassDirectory(classe) + file;
    }

    /**
     * Gets the class directory.
     * 
     * @param c the c
     * 
     * @return the class directory
     */
    public static String getClassDirectory(final Class<?> c) {
        String classFullName = c.getName();
        final String className = classFullName.substring(classFullName.lastIndexOf('.') + 1)
                + CLASS_EXTENSION;
        classFullName = null;
        final URL u = c.getResource(className);
        final String dir = u.getPath().substring(0, u.getPath().lastIndexOf(className));
        return dir;
    }
}
